/**
 * Singleton's interface
 */
function Reg () {
}

/**
 * Static semi-constructor
 * @return object Reg
 */
Reg.instance = function() {
    return Rege1b3a483f4129c554d7e5bbf3f070604;
}

/**
 * Actual singleton class
 */
Rege1b3a483f4129c554d7e5bbf3f070604 = new function RegStorage() {

    this._storage            = new Array();
    this._overwrite_when_set = false;
    this._instance           = null;
    this._active_flag        = 'active';

    /**
     * Gets and optionally sets overwrite mode
     * @param $mode bool Optional
     * @return bool Current mode
     */
    this.overwrite = function( $mode ) {
        if ($mode==false || $mode==true) {
            this._overwrite_when_set = $mode;
        }
        return this._overwrite_when_set;
    }
    
    /**
     * Resets storage
     * 
     * @return void
     */
    this.reset = function() {
        this._storage = null;
        this._storage = new Array();
        this.set('active', 1);
        return;
    }

    /**
     * @param $key   string Key
     * @return mixed Null if key is not found
     */
    this.get = function( $key ) {
        return (this._storage[$key]!=null) ? this._storage[$key] : null; // in case we change our mind and want to return some more complex output on data not found
    }

    /**
     * Sets registry key
     * @param $key       string Key
     * @param $value     mixed  Content
     * @param $overwrite bool   optional, allows changing mode using setter
     * @return bool Operation status
     */
    this.set = function( $key, $value, $overwrite ) {
        if ($key==this._active_flag && this._storage[this._active_flag]!=null) {
            return false; // safety fuse
        }
        if (this._storage[$key]!=null && this.overwrite($overwrite)==false) {
            return false; // prevents accidental overwrite when overwrite set true
        } else {
            this._storage[$key] = $value;
            return (this._storage[$key]!=null);
        }
    }
}

/**
 * Here we set 'active' flag to value 'true', this flag is in 'restricted'
 * array of the wrapper and it is a read only one. While calling
 * Reg.instance() we can quickly check
 * Reg.instance().get('active')==true
 * that proves the singleton worked
 */
Rege1b3a483f4129c554d7e5bbf3f070604.set('active', true);



